/**
 * Created by Jackie.Chen on 2017/5/18.
 */
'use strict'

var headerTemplete = require('html!./header.html');
require('../../css/header.less');
module.exports = {
  template: headerTemplete,
  data:function(){
    return {
      isLogin:false,
      name:'张三'
    }
  },
  created:function(){
    this.getUserInfo();
  },
  watch: {
    // 如果路由有变化，会再次执行该方法
   // "$route": "getUserInfo"
  },
  methods:{
    getUserInfo:function () {
       var me = this;
      this.$http.get('/user/getUserInfo.do').then(function(res) {
        //获取用户信息
        console.log('获取用户信息请求后返回结果: ', res);
        if(res.data.status === 'OK') {
          if(res.data.result){
            me.isLogin = true;
            var userInfo = res.data.result;
            me.name = userInfo.username;
            localStorage.setItem("name",me.name);
            localStorage.setItem("lastLoginTime",userInfo.lastLoginTime);
            localStorage.setItem("userId",userInfo.userid);
            localStorage.setItem("token",userInfo.tokenValue);
          }else{
            me.isLogin = false;
            localStorage.removeItem('userId');
            localStorage.removeItem("name");
            localStorage.removeItem("lastLoginTime");
            localStorage.removeItem("token");
          }
        }else {
          layer.msg('系统繁忙，请稍后再试');
        }
      });
    },
    logout:function(){
      var me = this;
      this.$http.get('/user/loginOut.do').then(function(res) {
        //获取用户信息
        console.log('退出登陆请求后返回结果: ', res);
        if(res.data.status === 'OK') {
          layer.msg("退出登陆");
          me.isLogin = false;
          location.href="/user/login.html";
          localStorage.removeItem('userId');
          localStorage.removeItem("name");
          localStorage.removeItem("lastLoginTime");
          localStorage.removeItem("isModel");
          localStorage.removeItem("token");
        }else {
          layer.msg('目前未登录或登录超时，请重新登录！');
          location.href="/user/login.html";
        }
      });
    }
  }
};
